import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.atomic.AtomicInteger;

public class TimeClientHandler extends ChannelInboundHandlerAdapter {

    private AtomicInteger atomicInteger = new AtomicInteger();

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 这时已经是解码成功的，所以不需要像之前一样使用ByteBuf进行编码
        IMessage body = (IMessage) msg;
        if (MessageTypeEnum.PONG.equals(((IMessage) msg).getType()) || MessageTypeEnum.EMPTY.equals(((IMessage) msg).getType())) {
            return;
        }
        System.out.println(atomicInteger.addAndGet(1) + "---" + Thread.currentThread().getName() + ", " + body.getFrom() + " send Message : " + body.getBody());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }
}
